package game.scene;

import java.util.Collections;
import java.util.LinkedList;

import common.Coordinate;
import common.Rectangle;
import engine.video.blit.Blitter;
import engine.video.blit.PixelBuffer_I;
import game.scene.sceneobject.SceneObject_A;

public class SceneManager {
	
	public SceneManager() {
		// Construct layers based on enum values.
		_Layers = new Layer[Layer_E.LAYER_COUNT.Plane];
		
		Layer_E[] layerIds = Layer_E.values();
		for(int i = 0; i < Layer_E.LAYER_COUNT.Plane; i++) {
			_Layers[i] = new Layer(layerIds[i]);
		}
	}

	private Layer[] _Layers;
	
	public Layer getLayer(Layer_E id) {
		if(id == Layer_E.LAYER_COUNT) {
			return null;
		}
		return _Layers[id.Plane];
	}
	
	public void render(Rectangle frame, PixelBuffer_I buffer) {
		if(buffer == null) {
			throw new NullPointerException("SceneManager::render - Buffer cannot be null");
		}
		if(frame == null) {
			frame = new Rectangle(buffer.getWidth(), buffer.getHeight());
		}
		Blitter blitter = new Blitter(buffer);
		
		Coordinate camera;
		
		
		LinkedList<SceneObject_A> sceneObjects;
		for(Layer layer : _Layers) {
			if(layer.Visible == true) {
				camera = layer.Translation;
				frame.X = camera.X;
				frame.Y = camera.Y;
				sceneObjects = layer.getOverlapping(frame);
				for(SceneObject_A object : sceneObjects) {
					object.render(layer.Translation, blitter);
				}
			}
		}
	}
	
}
